package device

import (
	"common/logging"
	"encoding/json"
	"iot/iot_app_server/app/util"

	"net/http"
	"time"
)

//http://b2bgateway/scene/AddScene/123



func HandleDeviceDebug(w http.ResponseWriter, req *http.Request) {
	//解决跨域问题

	sTime := time.Now()
	w.Header().Add("Access-Control-Allow-Origin", "*")
	rmap := make(map[string]interface{})

	payloadInfo := &util.PayloadInfo{}
	_, err := util.GetReqBody(req, payloadInfo)
	if err != nil {
		w.Write(util.LogGetResponseString(req, sTime, "", util.ERR_CODE_PARA_ERROR, rmap))
		logging.Error("HandleTransport para error 1,GetReqBody failed,request=%v", req.Form)
		return
	}

	Deviceid := payloadInfo.Deviceid
	Info := string(payloadInfo.Data)

	//验证参数
	if Deviceid == "" || Info == "" {
		w.Write(util.LogGetResponseString(req, sTime, payloadInfo.Msgid, util.ERR_CODE_PARA_ERROR, rmap))
		logging.Error("HandleTransport para error 2,request=%v", req.Form)
		return
	}

	code, _, response := util.SyncPassThrough(payloadInfo, "/debug")
	if code != util.ERR_CODE_SUCCESS && code != util.ERR_CODE_DEVICE_RESPONSE_CODE {
		w.Write(util.LogGetResponseString(req, sTime, payloadInfo.Msgid, code, rmap))
	} else {
		var data interface{}
		json.Unmarshal(response.Data, &data)
		rmap["msg"] = response.Msg
		if data == nil {
			data = map[string]string{}
		}
		w.Write(util.LogGetResponseString_EX(req, sTime, response.Msgid, rmap, response.Code, data))
	}

	logging.Info("HandleTransport OK")

}
